home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15012 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  43 lines

  1. Path: pangea.Stanford.EDU!karish
  2. From: karish@pangea.Stanford.EDU (Chuck Karish)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: "Overlapping Objects"
  5. Date: 16 Apr 1996 17:17:49 GMT
  6. Organization: Mindcraft, Inc.
  7. Message-ID: <4l0knt$lr1@nntp.Stanford.EDU>
  8. References: <31714280.201A@micromedia.on.ca> <4ks0lo$b1n@inet-nntp-gw-1.us.oracle.com> <31739F0E.69B6@micromedia.on.ca>
  9. NNTP-Posting-Host: pangea.stanford.edu
  10.  
  11. In article <31739F0E.69B6@micromedia.on.ca>,
  12. David Williams  <dwilliam@micromedia.on.ca> wrote:
  13. >Given 
  14. >
  15. >    char s[]="abcdefghijklmnop";
  16. >    char *sp1=&s[2];
  17. >    char *sp2=&s[4];
  18. >
  19. >if you strcpy(sp1,sp2) [not strcat(), as below], will the results be 
  20. >as expected ("abefghijklmnop"), or will there be problems because the copy
  21. >is copying over part of what it was originally copying from?
  22.  
  23. The Standard C prototype for strcpy() looks like:
  24.  
  25.     char *strcpy(char *s1, const char *s2);
  26.  
  27. This guarantees that the data pointed to by s2 will not be
  28. modified by the call to strcpy().  Since this guarantee
  29. cannot be fulfilled if the strings overlap as shown above,
  30. the description of strcpy() says:
  31.  
  32.     If copying takes place between objects that ovewrlap, the
  33.     behavior is undefined.
  34.  
  35. The exact mechanism by which the strcpy is done is not specified
  36. in any standard I know of.  That means that while this code
  37. will probably work on some systems, it's not guaranteed to
  38. work everywhere.
  39. --
  40.  
  41.     Chuck Karish          karish@mindcraft.com
  42.     (415) 323-9000 x117   karish@pangea.stanford.edu
  43.